/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.azienda.managerworkstandard.modello;

import it.azienda.framework.modello.BeanBase;
import it.azienda.framework.persistenza.DAOConnectionSingleton;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;

/**
 *
 * @author micheleWork
 */
public class BeanCartaRegalo extends BeanBase {

    public BeanCartaRegalo() {
        this.data = new Date(System.currentTimeMillis());
    }
    private Integer id;
    private BeanCliente beanCliente = new BeanCliente();
    private java.util.Date data;
    private String note;
    private Double importo;
    private String codice;
    private Set regaloDet = new HashSet();
    private Double saldo = new Double(0);

    /**
     * @return the id
     */
    public Integer getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * @return the beanCliente
     */
    public BeanCliente getBeanCliente() {
        return beanCliente;
    }

    /**
     * @param beanCliente the beanCliente to set
     */
    public void setBeanCliente(BeanCliente beanCliente) {
        this.beanCliente = beanCliente;
    }

    /**
     * @return the data
     */
    public java.util.Date getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(java.util.Date data) {
        this.data = data;
    }

    /**
     * @return the note
     */
    public String getNote() {
        return note;
    }

    /**
     * @param note the note to set
     */
    public void setNote(String note) {
        this.note = note;
    }

    /**
     * @return the importo
     */
    public Double getImporto() {
        return importo;
    }

    /**
     * @param importo the importo to set
     */
    public void setImporto(Double importo) {
        this.importo = importo;
    }

    /**
     * @return the codice
     */
    public String getCodice() {
        return codice;
    }

    /**
     * @param codice the codice to set
     */
    public void setCodice(String codice) {
        this.codice = codice;
    }

    /**
     * @return the regaloDet
     */
    public Set getRegaloDet() {
        return regaloDet;
    }

    /**
     * @param regaloDet the regaloDet to set
     */
    public void setRegaloDet(Set regaloDet) {
        this.regaloDet = regaloDet;
    }

    /**
     * @return the saldo
     */
    public Double getSaldo() {
        
            if (id != null) {
                String query = "select sum(importo) from BeanCartaRegaloDet det"
                        + " where det.beanCartaRegalo.id = :id";
                Session sessione = DAOConnectionSingleton.newSession();
                try{
                Object obj = sessione.createQuery(query)
                        .setInteger("id", id.intValue())
                        .uniqueResult();       
                if(obj != null){
                    saldo = new Double(obj.toString());
                }
                }catch(Exception e ){
                    e.printStackTrace();
                }
            }
        
        
        if(saldo == null){
            saldo = importo;
        }else{
            saldo = new Double(importo.doubleValue() - saldo.doubleValue());
        }
        return saldo;
    }

    /**
     * @param saldo the saldo to set
     */
    public void setSaldo(Double saldo) {
        this.saldo = saldo;
    }

}
